<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Validator;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use App\Models\Admin\HomeUser;
class UserController extends Controller
{
    /**
     * 前台用户列表
     *
     * @return 用户展示页面
     */
    public function index(Request $request)
    {
        //接受搜索的值 
        $input = $request -> input('search')?$request -> input('search'):'';
        $count = $request -> input('count',10);
        $req = $request -> all();
        $caption = '前台用户管理';
        $block = '用户列表';
        $user = HomeUser::leftjoin('home_userinfo as us','home_user.id','=','us.u_id');;
        $user = $user->where('username','like','%'.$input.'%')->paginate($count);
        // dd($user);
        return view('admin.user.index',compact('caption','block','user','input','req'));
    }

    /**
     * 添加前台用户
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {       

        return view('admin.user.create',['caption'=>'前台用户管理','block'=>'用户添加']);
        //
    }

    /**
     *  添加用户逻辑代码
     *  
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        
        //接受添加的元素
         $entire = $request->except('_token');
         //表单验证
         $rule = [
            'username' => 'required|unique:home_user|regex:/^[a-zA-Z0-9_]{6,18}$/',
            'password' => 'required|regex:/^[a-zA-Z0-9_]{6,18}$/',
            'email'=>'email',
            'phone'=>'regex:/^1[34578]\d{9}$/',
        ];
        //表单验证提示错误消息
        $msg = [
            'username.required'=>'用户名不能为空',
            'username.unique'=>'用户名已存在',
            'username.regex'=>'用户名填写格式不规范',
            'password.required'=>'密码不能为空',
            'password.regex'=>'密码格式不规范',
            'email.email'=>'邮箱格式不正确',
            'phone.regex'=>'手机号不规范'
        ];

       $validator = Validator::make($entire,$rule,$msg);

        if ($validator->fails()) {
            return redirect('admin/user/create')
                        ->withErrors($validator)
                        ->withInput();
        }
        //将前台用户密码进行Hash加密
        $pass = Hash::make($entire['password']);
        $input = ['username'=>$entire['username'],'password'=>$pass,'email'=>$entire['email'],'phone'=>$entire['phone']];
        //将数据填入数据库 返回数据为对象形式
        $success = HomeUser::create($input);
        if($success){
            return redirect('admin/user')->with('success','添加成功')->withInput();
        }else{
            return back()->with('error','添加失败')->withInput();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     *  修改 
     *
     * @param  int  $id
     * @return 展示修改页面
     */
    public function edit($id)
    {
        //查询单条数据
        $user = HomeUser::find($id);
        $caption = '前台用户管理';
        $block = '修改用户';
        return view('admin.user.edit',compact('user','caption','block'));
    }

    /**
     * 修改数据逻辑
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //接受传递过来所有值
        $complete = $request ->except('_token','_method');
        //查找数据库里面的id的所有值
        $user = HomeUser::find($id);
        //判断原密码与输入的原密码是否一致
        $res = Hash::check($complete['password'],$user['password']);
        if($complete['newpass'] != $complete['repass']){
            return back()->with('error','二次输入的密码不一致');
        }
        //判断原用户名与数据库用户名是否冲突
        if($user['username'] == $complete['username']){
            return back()->with('error','用户名已存在');
        }
        //将新密码加密
        $newpass = Hash::make($complete['newpass']);
        //将新密码插入数组中
        $complete['password'] = $newpass;
        //重新赋值到新的数组中
        $completes['username'] = $complete['username'];
        $completes['password'] = $complete['password'];
        $completes['email'] = $complete['email'];
        $completes['phone'] = $complete['phone'];
        //查询数据库该字段
        $set = HomeUser::find($id);
        //修改传递过来的值
        $update = $set -> update($completes);
        //判断是否修改成功 成功返回到用户列表 失败返回到上一级
        if($update){
            return redirect('admin/user')->with('success','修改成功');
        }else{
            return back('admin/user/'.$id)->with('error','修改失败');
        }

    }

    /**
     * 删除用户
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = [];
        //将ajax传过来的值进行删除
        $res = HomeUser::find($id)->delete();
        //判断传过来的id是否删除成功
        if($res){
            $data = [
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data = [
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }
        //返回一个数组 
        return $data;
    }
}
